package com.sskj.cndragon.ui.activity;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.sskj.cndragon.R;
import com.sskj.cndragon.presenter.OrderManagerActivityPresenter;
import com.sskj.cndragon.ui.fragment.BuyInFragment;
import com.sskj.cndragon.ui.fragment.PositionFragment;
import com.sskj.cndragon.ui.fragment.SellOutFragment;
import com.sskj.cndragon.view.ToolBarLayout;
import com.sskj.common.adapter.MyFragmentPagerAdapter;
import com.sskj.common.base.App;
import com.sskj.lib.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class OrderManagerActivity extends BaseActivity<OrderManagerActivityPresenter> {
    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.segmentTabLayout)
    SegmentTabLayout segmentTabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    private String[] mTitles = {App.INSTANCE.getString(R.string.mairu),
            App.INSTANCE.getString(R.string.chicang),
            App.INSTANCE.getString(R.string.maichu)};

    private List<Fragment> fragmentList;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_order_manager;
    }

    @Override
    public OrderManagerActivityPresenter getPresenter() {
        return new OrderManagerActivityPresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        toolbar.setLeftButtonOnClickListener(view -> finish());
        segmentTabLayout.setTabData(mTitles);
        fragmentList = new ArrayList<>();
        fragmentList.add(BuyInFragment.newInstance());
        fragmentList.add(PositionFragment.newInstance());
        fragmentList.add(SellOutFragment.newInstance());
        segmentTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewPager.setCurrentItem(position, true);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList));

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                segmentTabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, OrderManagerActivity.class);
        context.startActivity(intent);
    }
}
